﻿using Fierce.Core.Aop;
using Fierce.Entity.Basic;
using Fierce.Utility.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Html;
using Fierce.MvcExtend.Controllers;

namespace Fierce.MvcExtend.Controls.Manage
{
    public static class Easyui
    {
        #region 按钮模块

        /// <summary>
        /// 自定义按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="func">方法</param>
        /// <returns></returns>
        public static IHtmlContent LinkButton(string icon, string text, string mtag, string func, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append($" class=\"easyui-linkbutton {mtag}func\"");

            if (plain)
            {
                sb.Append(" plain=\"true\"");
            }
            if (!string.IsNullOrEmpty(icon))
            {
                sb.Append($" iconCls=\"{icon}\"");
            }

            sb.Append($" data-func=\"{func}\"");
            sb.Append($">{text}</a>");

            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// 自定义图标按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="func">方法</param>
        /// <returns></returns>
        public static IHtmlContent IconButton(string icon, string text, string mtag, string func, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append($" class=\"easyui-linkbutton {mtag}func\"");

            if (plain)
            {
                sb.Append(" plain=\"true\"");
            }

            sb.Append($" iconCls=\"{icon}\"");
            sb.Append($" data-func=\"{func}\"");
            sb.Append($" title=\"{text}\"></a>");

            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// 自定义菜单按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="menu">菜单ID</param>
        /// <returns></returns>
        public static IHtmlContent MenuButton(string icon, string text, string menu, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append(" class=\"easyui-menubutton\"");

            if (plain)
            {
                sb.Append(" plain=\"true\"");
            }
            if (!string.IsNullOrEmpty(icon))
            {
                sb.Append($" iconCls=\"{icon}\"");
            }
            if (!string.IsNullOrEmpty(menu))
            {
                sb.Append($" menu=\"{menu}\"");
            }

            sb.Append($">{text}</a>");

            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// 自定义图标菜单按钮
        /// </summary>
        /// <param name="icon">图标</param>
        /// <param name="text">文本</param>
        /// <param name="menu">菜单ID</param>
        /// <returns></returns>
        public static IHtmlContent IconMenuButton(string icon, string text, string menu, bool plain)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<a href=\"javascript:void(0)\"");
            sb.Append(" class=\"easyui-menubutton\"");

            if (plain)
            {
                sb.Append(" plain=\"true\"");
            }
            if (!string.IsNullOrEmpty(icon))
            {
                sb.Append($" iconCls=\"{icon}\"");
            }
            if (!string.IsNullOrEmpty(menu))
            {
                sb.Append($" menu=\"{menu}\"");
            }

            sb.Append($" title=\"{text}\"></a>");

            return new HtmlString(sb.ToString());
        }

        #endregion

        #region 菜单模块

        /// <summary>
        /// 后台主菜单
        /// </summary>
        /// <returns></returns>
        public static IHtmlContent MainMenu(string userid, bool isSuper)
        {
            var menus = ServiceFactory.BasicMenu.GetByUserId(userid, isSuper);

            StringBuilder sb = new StringBuilder();

            foreach (var model in menus)
            {
                if (model.Pid.Equals("0") && model.Type.Equals("null"))
                {
                    sb.Append($"<div class=\"theme-left-menu-title\">{model.Name}</div>");
                    sb.Append($"<div class=\"theme-left-menu-items\">");
                    sb.Append(MainChildrenMenus(model.ID, menus));
                    sb.Append("</div>");
                }
            }

            return new HtmlString(sb.ToString());
        }

        private static string MainChildrenMenus(string pid, List<BasicMenu> menus)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var item in menus)
            {
                if (item.Pid.Equals(pid))
                {
                    if (item.Type.Equals("href") || item.Type.Equals("iframe") || item.Type.Equals("dialog"))
                    {
                        if (item.Type.Equals("href"))
                        {
                            item.Link += $"?_menuid={item.ID}";
                        }

                        sb.Append("<a href=\"javascript:;\" ");
                        sb.Append("class=\"theme-left-menu-item\" ");
                        sb.Append($"data-href=\"{item.Link}\" ");
                        sb.Append($"data-type=\"{item.Type}\" ");
                        sb.Append($"data-icon=\"{item.Icon}\" ");
                        sb.Append($"onclick=\"manage.menuOnClick(this)\">{item.Name}</a>");
                    }
                }
            }

            return sb.ToString();
        }

        #endregion

        #region 页面模块

        /// <summary>
        /// Radio项
        /// </summary>
        /// <param name="name">表单名称</param>
        /// <param name="value">样本</param>
        /// <param name="text">文本</param>
        /// <param name="content">值</param>
        /// <returns></returns>
        public static IHtmlContent RadioItem(string name, string value, string text, string content)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"<input name=\"{name}\" type=\"radio\" ");
            sb.Append($"value=\"{value}\" label=\"{text}\" ");
            if (value.Equals(content))
            {
                sb.Append("checked=\"checked\" ");
            }
            sb.Append("/>");

            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// Check项
        /// </summary>
        /// <param name="name">表单名称</param>
        /// <param name="value">样本</param>
        /// <param name="text">文本</param>
        /// <param name="content">值</param>
        /// <returns></returns>
        public static IHtmlContent CheckItem(string name, int value, string text, int[] content)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"<input name=\"{name}\" type=\"checkbox\" ");
            sb.Append($"value=\"{value}\" label=\"{text}\" ");
            if (content.Contains(value))
            {
                sb.Append("checked=\"checked\" ");
            }
            sb.Append("/>");

            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// Check项
        /// </summary>
        /// <param name="name">表单名称</param>
        /// <param name="value">样本</param>
        /// <param name="text">文本</param>
        /// <param name="content">值</param>
        /// <returns></returns>
        public static IHtmlContent CheckItem(string name, string value, string text, string[] content)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"<input name=\"{name}\" type=\"checkbox\" ");
            sb.Append($"value=\"{value}\" label=\"{text}\" ");
            if (content.Contains(value))
            {
                sb.Append("checked=\"checked\" ");
            }
            sb.Append("/>");

            return new HtmlString(sb.ToString());
        }

        /// <summary>
        /// 内嵌页面
        /// </summary>
        /// <param name="tag">页面标记</param>
        /// <returns></returns>
        public static IHtmlContent Embed(string menuid, string tag)
        {
            BasicMenu embed = ServiceFactory.BasicMenu.GetEmbed(menuid, tag);

            string link = string.Empty;

            if (embed != null)
            {
                link = $"{embed.Link}?_menuid={embed.ID}";
            }

            return new HtmlString(link);
        }

        /// <summary>
        /// 页面功能
        /// </summary>
        /// <returns></returns>
        public static IHtmlContent Toolbar(WorkController work, bool plain = false)
        {
            var list = ServiceFactory.BasicFunction.GetByUserId(work.Menu.ID, work.UserID, work.IsSuper);

            StringBuilder sb = new StringBuilder();

            foreach (var item in list)
            {
                sb.Append("<a href=\"javascript:void(0)\"");
                sb.Append($" class=\"easyui-linkbutton {work.Menu.Module + work.Menu.Tag}func\"");

                if (plain)
                {
                    sb.Append(" plain=\"true\"");
                }
                if (!string.IsNullOrEmpty(item.Icon))
                {
                    sb.Append($" iconCls=\"{item.Icon}\"");
                }

                sb.Append($" data-func=\"{item.Func}\"");
                sb.Append($">{item.Name}</a>");
            }

            return new HtmlString(sb.ToString());
        }

        #endregion
    }
}